package com.eslserver.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FormatUtil {
	private static final String XML_DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
	private static final String DATE_PATTERN_US = "MM-dd-yyyy";
	private static final String TIME_PATTERN = "HH:mm";
	private static final String SIMPLE_DATE_PATTERN_TIMESTAMP = "MMddyyyy-HH:mm:ss";
	private static final String US_DATE_PATTERN_TIMESTAMP = "MM-dd-yyyy-HH:mm";
	private static final String DISPLAY_DATE_PATTERN_TIMESTAMP = "MMM d, yyyy-h:mm a";
	private static final String CURRENCY_PATTERN = "$#,#00.00";

	public static NumberFormat getCurrencyFormatInstance() {
		NumberFormat f = NumberFormat.getInstance();
		if (f instanceof DecimalFormat) {
			((DecimalFormat) f).applyPattern(CURRENCY_PATTERN);
		}
		return f;
	}

	public static DateFormat getXmlDateFormat() {
		DateFormat df = SimpleDateFormat.getInstance();
		((SimpleDateFormat) df).applyPattern(XML_DATE_PATTERN);
		return df;
	}

	public static String getXmlDate(Date date) {
		if (date == null) {
			return null;
		}
		DateFormat df = getXmlDateFormat();
		return df.format(date);
	}

	public static DateFormat getDateFormatUS() {
		DateFormat df = SimpleDateFormat.getInstance();
		((SimpleDateFormat) df).applyPattern(DATE_PATTERN_US);
		return df;
	}

	public static String getUSDate(Date date) {
		DateFormat df = getDateFormatUS();
		return df.format(date);
	}

	public static DateFormat getTimeFormat() {
		DateFormat df = SimpleDateFormat.getInstance();
		((SimpleDateFormat) df).applyPattern(TIME_PATTERN);
		return df;
	}

	public static String getTime(Date date) {
		DateFormat df = getTimeFormat();
		return df.format(date);
	}

	public static DateFormat getSimpleDateFormatTimestamp() {
		DateFormat df = SimpleDateFormat.getInstance();
		((SimpleDateFormat) df).applyPattern(SIMPLE_DATE_PATTERN_TIMESTAMP);
		return df;
	}

	public static DateFormat getUSDateFormatTimestamp() {
		DateFormat df = SimpleDateFormat.getInstance();
		((SimpleDateFormat) df).applyPattern(US_DATE_PATTERN_TIMESTAMP);
		return df;
	}

	public static String getSimpleDateTimestamp(Date date) {
		DateFormat df = getSimpleDateFormatTimestamp();
		return df.format(date);
	}

	public static DateFormat getDisplayDateFormatTimestamp() {
		DateFormat df = SimpleDateFormat.getInstance();
		((SimpleDateFormat) df).applyPattern(DISPLAY_DATE_PATTERN_TIMESTAMP);
		return df;
	}

	public static String getDisplayDateTimestamp(Date date) {
		DateFormat df = getDisplayDateFormatTimestamp();
		return df.format(date);
	}

}